package com.zzt.common.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class BaseAction extends ActionSupport {
	
	public static final int BUFFER_SIZE = 16 * 1024; 
	protected ActionContext actionContext;
	protected Map<String, Object> session;
	protected Map<String, Object> request;
	protected Map<String, Object> application;
	
	public ActionContext getActionContext() {
		if(actionContext == null)
			actionContext = ActionContext.getContext();
		return actionContext;
	}
	public Map<String, Object> getSession() {
		if(session == null)
			session = getActionContext().getSession();
		return session;
	}
	@SuppressWarnings("unchecked")
	public Map<String, Object> getRequest() {
		if(request == null)
			request = (Map<String, Object>)getActionContext().get("request");
		return request;
	}
	public Map<String, Object> getApplication() {
		if(application == null)
			application = (Map<String, Object>)getActionContext().getApplication();
		return application;
	}
	

	 protected void copy(File src, File dst)  {
         try  {
            InputStream in = null ;
            OutputStream out = null ;
             try  {                
                in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
                out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
                 byte [] buffer = new byte [BUFFER_SIZE];
                 while (in.read(buffer) > 0 )  {
                    out.write(buffer);
                } 
             } finally  {
                 if ( null != in)  {
                    in.close();
                } 
                  if ( null != out)  {
                    out.close();
                } 
            } 
         } catch (Exception e)  {
            e.printStackTrace();
        } 
    } 
    
     protected String getExtention(String fileName)  {
         int pos = fileName.lastIndexOf(".");
         return fileName.substring(pos);
    } 
 
    
//	@SuppressWarnings("unchecked")
//	protected LinkedHashMap getMapByAction(int limit) {
//		HttpServletRequest re = ServletActionContext.getRequest();
//
//		LinkedHashMap map = new LinkedHashMap();
//		Enumeration it = re.getParameterNames();
//		while (it.hasMoreElements()) {
//			String key = it.nextElement().toString();
//			map.put(key, re.getParameter(key));
//		}
//		if (!map.containsKey("start")) {
//			map.put("start", Integer.valueOf(0));
//		}
//		if (!map.containsKey("limit")) {
//			map.put("limit", Integer.valueOf(limit));
//		}
//		return map;
//	}
}